﻿using Caliburn.Micro;
using GetMan.Core.Extensions;
using System;
using System.Threading.Tasks;

namespace GetMan.Core.infrastructure.BaseC
{
    public class CustomConductorBase<T1, T2> : Conductor<T2>.Collection.OneActive, IViewModelBase where T1 : class, new() where T2 : class
    {
        protected readonly NLog.Logger _logger;
        protected Type childType;
        /// <summary>
        /// 继承类名称
        /// </summary>
        public string ChildTypeName
        {
            get; set;
        }
        public CustomConductorBase()
        {
            _logger = NLog.LogManager.GetCurrentClassLogger();
            Loaded();
        }

        ~CustomConductorBase()
        {
            UnLoaded();
        }

        public virtual void Loaded()
        {
            childType = typeof(T1);
            ChildTypeName = childType?.Name;
            _logger.Info($"加载视图模型类：{ChildTypeName}");
        }

        public virtual void UnLoaded()
        {
            _logger.Info($"释放视图模型类：{ChildTypeName}");
            childType = null;
            ChildTypeName = null;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="methodName"></param>
        public virtual void AddLog(string message, string methodName = "")
        {
            if (string.IsNullOrWhiteSpace(methodName))
            {
                _logger.Info($"{ChildTypeName}|{message}");
                return;
            }
            _logger.Info($"{ChildTypeName}|{methodName}|{message}");
        }
    }


    public class ViewModelBase<T> : Screen, IViewModelBase where T : class
    {
        protected readonly NLog.Logger _logger;
        protected Type childType;


        /// <summary>
        /// 继承类名称
        /// </summary>
        public string ChildTypeName
        {
            get; set;
        }
        public ViewModelBase()
        {
            _logger = NLog.LogManager.GetCurrentClassLogger();
            Loaded();
        }

        ~ViewModelBase()
        {
            UnLoaded();
        }

        public virtual void Loaded()
        {
            childType = typeof(T);
            ChildTypeName = childType?.Name;
            _logger.Info($"加载视图模型类：{ChildTypeName}");
        }

        public virtual void UnLoaded()
        {
            _logger.Info($"释放视图模型类：{ChildTypeName}");
            childType = null;
            ChildTypeName = null;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="methodName"></param>
        public virtual void AddLog(string message, string methodName = "")
        {
            if (string.IsNullOrWhiteSpace(methodName))
            {
                _logger.Info($"{ChildTypeName}|{message}");
                return;
            }
            _logger.Info($"{ChildTypeName}|{methodName}|{message}");
        }

        public async Task<T> ExecuteAsync<T>(Func<Task<T>> func)
        {
            try
            {
                return await func.Invoke();
            }
            catch (Exception ex)
            {
                if (ex is FriendlyException e)
                {
                    Rubyer.Message.Error(e.Message);
                    return default(T);
                }
                AddLog(ex.Message, nameof(ExecuteAsync));
                return default(T);
            }
        }
    }

    public interface IViewModelBase
    {
        void Loaded();
        void UnLoaded();
        void AddLog(string message, string methodName = "");
    }
}